﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    public sealed class SubControl : AddControl
    {




        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            
        }



        protected override void Timer_Tick(object sender, object e)
        {

            if (oneConnectElement == null || twoConnectElement == null)
            {
                return;
            }

            GetOneExportValue();
            GetTwoExportValue();

            try
            {

                if (oneExportValue is double)
                {
                    if (twoExportValue is double)
                    {
                        Value = (double)oneExportValue - (double)twoExportValue;
                        Type = OutputType.Double;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector2.Subtract(new Vector2((float)oneExportValue), (Vector2)twoExportValue);
                        Type = OutputType.Vector2;

                    }
                    else if (twoExportValue is Vector3)
                    {

                        Value = Vector3.Subtract(new Vector3((float)oneExportValue), (Vector3)twoExportValue);
                        Type = OutputType.Vector3;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        Value = Vector4.Subtract(new Vector4((float)oneExportValue), (Vector4)twoExportValue);
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Color)
                    {
                        Value = Vector4.Subtract(new Vector4((float)oneExportValue), (Vector4)twoExportValue);
                        Type = OutputType.Vector4;
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector2)
                {
                    if (twoExportValue is double)
                    {
                        Value = Vector2.Subtract((Vector2)oneExportValue, new Vector2((float)twoExportValue));
                        Type = OutputType.Vector2;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector2.Subtract((Vector2)oneExportValue, (Vector2)twoExportValue);
                        Type = OutputType.Vector2;

                    }
                    else if (twoExportValue is Vector3)
                    {
                        var v3 = (Vector2)twoExportValue;
                        Value = Vector2.Subtract((Vector2)oneExportValue, new Vector2(v3.X, v3.Y));
                        Type = OutputType.Vector2;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector2.Subtract((Vector2)oneExportValue, new Vector2(v4.X, v4.X));
                        Type = OutputType.Vector2;
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector3)
                {
                    if (twoExportValue is double)
                    {
                        Value = Vector3.Subtract((Vector3)oneExportValue, new Vector3((float)twoExportValue));
                        Type = OutputType.Vector3;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector3.Subtract((Vector3)oneExportValue, new Vector3((Vector2)twoExportValue, 0));
                        Type = OutputType.Vector3;

                    }
                    else if (twoExportValue is Vector3)
                    {

                        Value = Vector3.Subtract((Vector3)oneExportValue, (Vector3)twoExportValue);
                        Type = OutputType.Vector3;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector3.Subtract((Vector3)oneExportValue, new Vector3(v4.X, v4.Y, v4.Z));
                        Type = OutputType.Vector3;
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector4)
                {
                    if (twoExportValue is double)
                    {
                        Value = Vector4.Subtract((Vector4)oneExportValue, new Vector4((float)twoExportValue));
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector4.Subtract((Vector4)oneExportValue, new Vector4((Vector2)twoExportValue, 0, 0));
                        Type = OutputType.Vector4;

                    }
                    else if (twoExportValue is Vector3)
                    {
                        Value = Vector4.Subtract((Vector4)oneExportValue, new Vector4((Vector3)twoExportValue, 0));
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector4.Subtract((Vector4)oneExportValue, v4);
                        Type = OutputType.Vector4;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }



            }
            catch
            {
                twoConnectElement.Exporter.Disconnect(twoConnectElement);
                
            }
        }



        void GetOneExportValue()
        {
            if (oneConnectElement != null)
            {
                try
                {
                    oneExportValue = GetProvideValue(oneConnectElement.Exporter, oneExportValueName);
                }
                catch
                {
                    oneConnectElement.Exporter.Disconnect(oneConnectElement);
                }
            }
        }

        private void GetTwoExportValue()
        {
            if (twoConnectElement != null)
            {
                try
                {
                    twoExportValue = GetProvideValue(twoConnectElement.Exporter, twoExportValueName);
                }
                catch
                {
                    twoConnectElement.Exporter.Disconnect(twoConnectElement);
                }
            }
        }


      
    }
}
